/*
 * @description: 几何简化命令弹窗管理器
 * @author: YinJianFeng
 * @version: 1.0.0
 * @date: 2024-11-06 16:18:05
 */

import { TopoEvents } from "@app-cad/environments/nextmesh/types";
import { userEvents } from "@app-cad/module";
import { ReactNode, useEffect, useState } from "react";

export const TopoCmdDialogHolder = () => {
  const [topoCmdDialog, setTopoCmdDialog] = useState<ReactNode>(null);

  useEffect(() => {
    registerEvents();
    return () => {
      unregisterEvents();
    };
  }, []);

  const registerEvents = () => {
    userEvents.on(TopoEvents.SHOW_TOPO_DIALOG, (cmdDialog: ReactNode) => {
      showTopoCmdDialog(cmdDialog);
    });
    userEvents.on(TopoEvents.HIDE_TOPO_DIALOG, () => {
      hideTopoCmdDialog();
    });
  };
  const unregisterEvents = () => {
    userEvents.off(TopoEvents.SHOW_TOPO_DIALOG, showTopoCmdDialog);
    userEvents.off(TopoEvents.HIDE_TOPO_DIALOG, hideTopoCmdDialog);
  };

  const showTopoCmdDialog = (cmdDialog: ReactNode) => {
    setTopoCmdDialog(cmdDialog);
  };
  const hideTopoCmdDialog = () => {
    setTopoCmdDialog(null);
  };

  return <>{topoCmdDialog}</>;
}